<?php
/**
 * Created by PhpStorm.
 * User: wdt
 * Date: 2016/4/18
 * Time: 13:44
 */

namespace front\modules\web\controllers;


use common\helpers\ReturnHelper;
use front\controllers\RestBaseController;
use coupons\services\CouponsGroupService;
use front\controllers\WebBaseController;
use yii\helpers\ArrayHelper;

class CouponsController extends RestBaseController{

    /**
     * 获取卡券信息即获取卡券组信息
     */
    public function actionGetInfo()
    {
        $coupons_group_id = \Yii::$app->request->post('coupons_group_id');
        if (empty($coupons_group_id)) {
            return ReturnHelper::error('未传递coupons_group_id参数');
        }
        $ret = CouponsGroupService::getGroupInfo($coupons_group_id);
        if ($ret['code'] == 0) {
            return ReturnHelper::success('success');
        }
        return ReturnHelper::error('获取卡券信息失败');
    }

    /**
     * 领取卡券
     */
    public function actionReceive()
    {
        $coupons_group_id = \Yii::$app->request->post('coupons_group_id');
        $user_id = \Yii::$app->request->post('user_id');
        if (empty($coupons_group_id)) {
            return ReturnHelper::error('未传递coupons_group_id');
        }
        if (empty($user_id)) {
            return ReturnHelper::error('未传递用户id');
        }
        $ret = CouponsGroupService::receive($coupons_group_id,$user_id);
        if ($ret['code'] == 0) {
            return ReturnHelper::success('success');
        }
        return ReturnHelper::error_json('领券卡券失败');
    }

    /**
     * 获取用户不同状态卡券列表
     */
    public function actionList()
    {
//        $user_id = $this->getUserId();
        $user_id = \Yii::$app->request->post('user_id');
        $status = 3;
        if (!empty($_GET['status'])) {
            $status = $_GET['status'];
        }
        if (empty($user_id)) {
            return ReturnHelper::error('未传递用户id');
        }
        if (empty($status)) {
            return ReturnHelper::error('未传递status参数');
        }

        //获取用户获取的卡券信息
        $ret = CouponsGroupService::getStatus($user_id,$status);
        if ($ret['code'] == 1) {
            return ReturnHelper::success('success',['coupons' => '']);
        }

        //根据卡券组id,获取卡券活动的具体信息
        $groupInfo = [];
        foreach ($ret['message'] as $k=>$v) {
            $info = CouponsGroupService::getGroupInfo($v['group_id']);
            $data['type'] = $info['result'][0]['type'];
            $data['coupons_status'] = $v['status'];
            $data['coupons_id'] = $v['coupons_id'];
            $data['groupInfo'] = $info['result'][0];
            array_push($groupInfo,$data);
        }

        //对象转换为数组
        $groupInfo = ArrayHelper::toArray($groupInfo);
        if ($ret['code'] == 0) {
            return ReturnHelper::success('success',['coupons' => $groupInfo]);
        }
        return ReturnHelper::error('获取用户卡券列表失败');
    }

    /**
     * 卡券列表页面
     * 获取不同状态的卡券信息
     */
    public function actionStatusList()
    {
        $user_id = \Yii::$app->request->post('user_id');
        $type = \Yii::$app->request->post('type');

        //已使用
        if ($type == 1) {

            //获取用户获取的卡券信息
            $ret = CouponsGroupService::getStatus($user_id,4);

            //根据卡券组id,获取卡券活动的具体信息
            $groupInfo = [];
            foreach ($ret['message'] as $k=>$v) {
                $info = CouponsGroupService::getGroupInfo($v['group_id']);
                $data['type'] = $info['result'][0]['type'];
                $data['coupons_status'] = $v['status'];
                $data['coupons_id'] = $v['coupons_id'];
                $data['groupInfo'] = $info['result'][0];
                array_push($groupInfo,$data);
            }

            //对象转换为数组
            $groupInfo = ArrayHelper::toArray($groupInfo);
            return ReturnHelper::success('success',['groupInfo' => $groupInfo]);
        }

        //未使用的卡券
        if ($type == 0) {

            //获取用户获取的卡券信息
            $ret = CouponsGroupService::getStatus($user_id,3);

            //根据卡券组id,获取卡券活动的具体信息
            $groupInfo = [];
            foreach ($ret['message'] as $k=>$v) {
                $info = CouponsGroupService::getGroupInfo($v['group_id']);
                $data['type'] = $info['result'][0]['type'];
                $data['coupons_status'] = $v['status'];
                $data['coupons_id'] = $v['coupons_id'];
                $data['groupInfo'] = $info['result'][0];
                array_push($groupInfo,$data);
            }

            //对象转换为数组
            $groupInfo = ArrayHelper::toArray($groupInfo);
            return ReturnHelper::success('success',$groupInfo);
        }
        return ReturnHelper::error('failed');
    }

    /**
     * 用户使用卡券
     */
    public function actionUse()
    {
        $data = \Yii::$app->request->post();
        if (!isset($data['user_id'])) {
            return ReturnHelper::error('未传递用户id');
        }
        if (!isset($data['order_id'])) {
            return ReturnHelper::error('未传递order_id');
        }
        if (!isset($data['coupons_id'])) {
            return ReturnHelper::error('未传递coupons_id');
        }
        $ret = CouponsGroupService::pay($data['user_id'],$data['coupons_id'],$data['order_id']);
        if ($ret['code'] == 0) {
            return ReturnHelper::success('success');
        }
        return ReturnHelper::error('failed',$ret['message']);
    }

}